package com.ruoyi.livedemo.controller;

import com.ruoyi.livedemo.common.ApiResponse;
import com.ruoyi.livedemo.context.CurrentUserHolder;
import com.ruoyi.livedemo.dto.GoalRequest;
import com.ruoyi.livedemo.dto.PagedResult;
import com.ruoyi.livedemo.entity.GoalItem;
import com.ruoyi.livedemo.service.GoalService;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/goals")
public class GoalController {

    private final GoalService goalService;

    @GetMapping
    public ApiResponse<PagedResult<GoalItem>> list(@RequestParam(required = false) String status,
                                                   @RequestParam(required = false) String category,
                                                   @RequestParam(defaultValue = "0") int page,
                                                   @RequestParam(defaultValue = "10") int size) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok(goalService.list(userId, status, category, page, size));
    }

    @PostMapping
    public ApiResponse<GoalItem> create(@Valid @RequestBody GoalRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("目标已创建", goalService.create(userId, request));
    }

    @PutMapping("/{id}")
    public ApiResponse<GoalItem> update(@PathVariable Long id, @Valid @RequestBody GoalRequest request) {
        Long userId = CurrentUserHolder.getUserId();
        return ApiResponse.ok("目标已更新", goalService.update(userId, id, request));
    }

    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        Long userId = CurrentUserHolder.getUserId();
        goalService.delete(userId, id);
        return ApiResponse.ok("目标已删除", null);
    }
}
